// pages/geren/xiangqing/xiangqing.js
var m = require("../../../utils/geren");

Page({
    data: {
        msg: m.msg,
        height: 0,
        userid: null,
        userInfo: {},
        scoreList: []
    },

    onLoad(options) {
        this.setData({
            height: wx.getSystemInfoSync().windowHeight - 50
        });

        // 获取用户ID并加载所有信息
        var that = this;
        wx.getStorage({
            key: "userid",
            success(res) {
                that.setData({
                    userid: res.data
                });
                that.getUserInfo();
                that.getScoreList();
            }
        });
    },

    getUserInfo() {
        var that = this;
        var database = wx.cloud.database();
        database.collection("user").where({
            _id: that.data.userid
        }).get().then((res) => {
            if (res.data.length > 0) {
                // 设置固定的昵称和专业班级信息
                let userInfo = res.data[0];
                userInfo.nickname = userInfo.name;  // 名称显示用户名
                userInfo.major = "软件工程";       // 固定专业
                userInfo.class = "22软件工程2班";  // 固定班级
                
                that.setData({
                    userInfo: userInfo
                });
            }
        }).catch(err => {
            console.error("获取用户信息失败：", err);
        });
    },

    getScoreList() {
        var that = this;
        var database = wx.cloud.database();
        database.collection("finalScore").where({
            userid: that.data.userid
        }).get().then((res) => {
            console.log("成绩列表：", res.data);
            that.setData({
                scoreList: res.data
            });
        }).catch(err => {
            console.error("获取成绩失败：", err);
        });
    }
});